﻿using System.ComponentModel;
using System.Threading.Tasks;

namespace Sidvall.LogManager.UI.Entities
{
    public class ServerManager : Sidvall.LogManager.ServerManager, INotifyPropertyChanged
    {
        #region Public Members

        public event PropertyChangedEventHandler PropertyChanged;

        #region InitializeException

        private System.Exception _InitializeException;
        public System.Exception InitializeException
        {
            get
            {
                return _InitializeException;
            }
            private set
            {
                _InitializeException = value;
                OnPropertyChanged("InitializeException");
                OnPropertyChanged("IsValid");
                OnPropertyChanged("StatusMessage");
            }
        }

        #endregion
        #region ServerConnectionId

        private int _ServerConnectionId;
        public int ServerConnectionId
        {
            get
            {
                return _ServerConnectionId;
            }
            set
            {
                _ServerConnectionId = value;
                OnPropertyChanged("ServerConnectionId");
            }
        }

        #endregion
        #region LogScopes

        private LogScopeCollection _LogScopes;
        public LogScopeCollection LogScopes
        {
            get
            {
                return _LogScopes;
            }
            set
            {
                _LogScopes = value;
                OnPropertyChanged("LogScopes");
            }
        }

        #endregion
        #region IsValid

        public bool IsValid
        {
            get
            {
                if (this.InitializeException != null)
                    return false;
                return true;
            }
        }

        #endregion
        #region StatusMessage

        public string StatusMessage
        {
            get
            {
                if (this.InitializeException != null)
                    return Sidvall.SystemContext.Current.SystemExceptionManager.GetMessage(this.InitializeException);
                return "OK";
            }
        }

        #endregion

        #region InitializeAsync

        public async Task InitializeAsync()
        {
            this.InitializeException = null;
            try
            {
                await InitializeAsync(this.ServerConnectionId);
                this.LogScopes = new LogScopeCollection();
                this.LogScopes.Import(this.LogScopeIndex.Values);
            }
            catch (System.Exception ex)
            {
                this.InitializeException = ex;
            }
        }

        #endregion
        #region OnPropertyChanged

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, args);
        }

        #endregion

        #endregion
    }
}
